#! /usr/bin/env python
import time
import socket
from pigredients.ics import ic74hc595 as ic74hc595

HIGH = ic74hc595.HIGH
LOW = ic74hc595.LOW

number_of_registers = 3

my_register = ic74hc595.Shift_Register(registers_in_chain=number_of_registers)


# Set up a UDP server
UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

# Listen on port 21567
# (to all IP addresses on this system)
listen_addr = ("",888)
UDPSock.bind(listen_addr)


while True:
	try:
	        client_socket, address = UDPSock.recvfrom(1024)
        	origrecdata = client_socket.strip()
		cmdlist = origrecdata.split(",")
		print cmdlist
		my_register.set_off()
		my_register.set_pin(pin_id=0, state=cmdlist[0])
		my_register.set_pin(pin_id=1, state=cmdlist[1])
		my_register.set_pin(pin_id=2, state=cmdlist[2])
		my_register.set_pin(pin_id=3, state=cmdlist[3])
		my_register.set_pin(pin_id=4, state=cmdlist[4])
		my_register.set_pin(pin_id=5, state=cmdlist[5])
		my_register.set_pin(pin_id=6, state=cmdlist[6])
		my_register.set_pin(pin_id=7, state=cmdlist[7])
		my_register.set_pin(pin_id=8, state=cmdlist[8])
		my_register.set_pin(pin_id=9, state=cmdlist[9])
		my_register.set_pin(pin_id=10, state=cmdlist[10])
		my_register.set_pin(pin_id=11, state=cmdlist[11])
		my_register.set_pin(pin_id=12, state=cmdlist[12])
		my_register.set_pin(pin_id=13, state=cmdlist[13])
		my_register.set_pin(pin_id=14, state=cmdlist[14])
		my_register.set_pin(pin_id=15, state=cmdlist[15])
		my_register.set_pin(pin_id=16, state=cmdlist[16])
		my_register.set_pin(pin_id=17, state=cmdlist[17])
		my_register.set_pin(pin_id=18, state=cmdlist[18])
		my_register.set_pin(pin_id=19, state=cmdlist[19])
		my_register.set_pin(pin_id=20, state=cmdlist[20])
		my_register.set_pin(pin_id=21, state=cmdlist[21])
		my_register.set_pin(pin_id=22, state=cmdlist[22])
		my_register.write()
		print "Finished"
	except Exception,e:
		print e
		pass
#my_register.all_off()

